
#ifndef MessengerPanel_H
#define MessengerPanel_H

#include <wx/aui/aui.h>
#include <wx/event.h>
#include <wx/richtext/richtextctrl.h>
#include <wx/artprov.h>
#include <wx/fontdlg.h>
#include <wx/progdlg.h>
#include <wx/splitter.h>

#include "tux.h"

#include "FileTransferManager.h"
#include "MsnMessenger.h"

class MsnMessenger;
class MsnSession;

class TuxNotebook;
class SessionNotebook;
class MessengerNotebook;

class ContactTreeCtrl;
class LoginPanel;
class ContactPanel;
class SessionPanel;


BEGIN_DECLARE_EVENT_TYPES()
	//As wxNotebook doesn't provide page close event, we have to notify main frame by this event
	DECLARE_EVENT_TYPE(EVT_MESSENGER_PANEL_CLOSED, -1)

END_DECLARE_EVENT_TYPES()


class MessengerPanel : public wxPanel
{

friend class LoginPanel;
friend class ContactPanel;
friend class SessionPanel;
friend class ContactTreeCtrl;

private:
    MsnMessenger* messenger;
	MessengerNotebook* messengerNotebook;

private:
	wxSplitterWindow* splitter;

	wxPanel* leftPanel;

	SessionNotebook* sessionNotebook;

    LoginPanel* loginPanel;
    ContactPanel* contactPanel;

	ContactTreeCtrl* contactTree;

	wxTimer *pingTimer;
	wxTimer *checkTimer;

public:
    MessengerPanel(wxWindow* parent);
    ~MessengerPanel();

	void LocateFocus();

	void UpdateUIText();

	MsnMessenger* GetMessenger(){return messenger;}

	bool IsAlive(){return messenger != NULL && messenger->IsAlive();}

	int GetMessengerStatus()
	{
		if(messenger == NULL){
			return MESSENGER_NEW;
		}
		return messenger->GetMessengerStatus();
	}

private:
	void CreateUI();

	void StartTimer();
	void StopTimer();

	void SwitchControlPanel();

	void OnSessionNotebookChanged(wxNotebookEvent& event);

	void OnTimerPing(wxTimerEvent& e);

	void OnTimerCheck(wxTimerEvent& e);

	void UpdateSessionTitle(MsnSession* session);

	void EnableSessionPanel(bool b);

public:
	void SignIn(const wxString& passport, const wxString& password, const wxString& status);
	void SignOut();


	void ShowUp(int pageIndex);

	bool HasNewMessage();

	SessionPanel* CurrentSessionPanel();


	SessionPanel* FindSessionPanel(const wxString& contact);
	SessionPanel* FindSessionPanel(MsnSession* session);
	SessionPanel* OpenSessionPanel(MsnSession* session, bool selected);
	SessionPanel* OpenSessionPanel(const wxString& contact, bool selected);



private: //msn event
	void OnConnectionFailed(wxCommandEvent& event);

	void OnConnectionBroken(wxCommandEvent& event);

	void OnAuthenticationFailed(wxCommandEvent& event);

	void OnSignedIn(wxCommandEvent& event);
	void OnSignedOut(wxCommandEvent& event);

	void OnUserNameChanged(wxCommandEvent& event);
	void OnUserStatusChanged(wxCommandEvent& event);

	void OnContactSynchronizationFinished(wxCommandEvent& event);

	void OnContactNameChanged(wxCommandEvent& event);
	void OnContactStatusChanged(wxCommandEvent& event);

	void OnContactAdded(wxCommandEvent& event);
	void OnContactRemoved(wxCommandEvent& event);

	void OnContactAddedToReversedList(wxCommandEvent& event);

	void OnContactAddedToBlockedList(wxCommandEvent& event);
	void OnContactRemovedFromBlockedList(wxCommandEvent& event);

	void OnContactAddedToGroup(wxCommandEvent& event);
	void OnContactRemovedFromGroup(wxCommandEvent& event);

	void OnGroupAdded(wxCommandEvent& event);
	void OnGroupRemoved(wxCommandEvent& event);
	void OnGroupRenamed(wxCommandEvent& event);



	void OnSessionContactJoined(wxCommandEvent& event);
	void OnSessionContactListed(wxCommandEvent& event);
	void OnSessionContactLeft(wxCommandEvent& event);

	void OnControlMessageReceived(wxCommandEvent& event);

	void OnInstantMessageReceived(wxCommandEvent& event);
	void OnInstantMessageFailed(wxCommandEvent& event);

	void OnFileInviteSent(wxCommandEvent& event);
	void OnFileInviteReceived(wxCommandEvent& event);

	void OnFileTransferStarted(wxCommandEvent& event);
	void OnFileTransferUpdated(FileTransferEvent& event);
	void OnFileTransferDone(wxCommandEvent& event);
	void OnFileTransferFailed(wxCommandEvent& event);
	void OnFileTransferCancelled(wxCommandEvent& event);

DECLARE_EVENT_TABLE()
};

#endif

